package com.phkj.qdh.ui.fragment.user

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.commit
import androidx.lifecycle.ViewModelProvider
import com.google.gson.reflect.TypeToken
import com.phkj.qdh.bean.DraftBean
import com.phkj.qdh.bean.ThreeScoreBean
import com.phkj.qdh.databinding.FragmentUserBinding
import com.phkj.qdh.ui.fragment.drafts.MyDraftsFragment
import com.phkj.qdh.ui.fragment.myfailover.MyFailoverFragment
import com.phkj.qdh.ui.fragment.mynotice.MyNoticeFragment
import com.phkj.qdh.ui.fragment.mynotice.MyThreeScoreFragment
import com.phkj.qdh.utils.SPSUtils
import com.phkj.qdh.utils.SPS_DRAFTS
import com.phkj.qdh.utils.launchAndRepeatWithViewLifecycle
import com.phkj.qdh.utils.requestMain
import kotlinx.coroutines.flow.collectLatest
import rxhttp.wrapper.utils.GsonUtil

/**
 * 我的
 */
class UserFragment : Fragment() {
    private var _binding: FragmentUserBinding? = null
    private val binding get() = _binding!!
    private lateinit var vm: UserVM
    private var threeScoreBean: ThreeScoreBean? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {

        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentUserBinding.inflate(inflater, container, false)
        vm = ViewModelProvider(this, getUserVMProvider())[UserVM::class.java]
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.run {
            srl.setOnRefreshListener {
                vm.getThreeScore()
                vm.getUserCenter()
            }
            rlTonggao.setOnClickListener {
                parentFragmentManager.commit {
                    addToBackStack("my_notice")
                    replace(id, MyNoticeFragment.newInstance("", ""))
                    setReorderingAllowed(true)
                }
            }
            three.setOnClickListener {
                threeScoreBean?.let { threeScoreBean ->
                    parentFragmentManager.commit {
                        addToBackStack("my_notice")
                        replace(id, MyThreeScoreFragment.newInstance(threeScoreBean, ""))
                        setReorderingAllowed(true)
                    }
                }

            }
            rlRongcuo.setOnClickListener {
                parentFragmentManager.commit {
                    addToBackStack("my_failover")
                    replace(id, MyFailoverFragment.newInstance("", ""))
                    setReorderingAllowed(true)
                }
            }
            rlCaogao.setOnClickListener {
                parentFragmentManager.commit {
                    addToBackStack("draft")
                    replace(id, MyDraftsFragment.newInstance("", ""))
                    setReorderingAllowed(true)
                }
            }
        }

        launchAndRepeatWithViewLifecycle {
            vm.state.collectLatest {
                binding.srl.isRefreshing = false
            }
        }

        launchAndRepeatWithViewLifecycle {
            vm.threeScore.collectLatest {
                binding.scroe.text = it.currentPoints.toString()
                threeScoreBean = it
            }
        }

        launchAndRepeatWithViewLifecycle {
            vm.userCenter.collectLatest {
                binding.tvTonggao.text = "${it.noticeNumber}"
                binding.tvPhone.text = "${it.phoneNumber}"
                binding.tvName.text = "${it.name}"
                binding.tvRongcuo.text = "${it.faultTolerantNumber}"
                binding.tvXuncha.text = "${it.eventNumber}"
            }
        }



    }

    fun dododod(){
        requestMain {
            try {
                val localDraft = GsonUtil.fromJson<ArrayList<DraftBean>>(
                    SPSUtils.getString(
                        SPS_DRAFTS, ""
                    ), object : TypeToken<ArrayList<DraftBean>>() {}.type
                )
                binding.tvCaogao.text = localDraft.size.toString()
            }catch (e:Exception){
                e.printStackTrace()
                binding.tvCaogao.text ="0"
            }
        }
    }

    override fun onResume() {
        super.onResume()
        vm.getThreeScore()
        vm.getUserCenter()
        dododod()
    }

    fun showLoacl(){
        dododod()
    }

    companion object {
        @JvmStatic
        fun newInstance() = UserFragment().apply {
            arguments = Bundle().apply {}
        }
    }
}